﻿using ApplicationModels.A03.Photographs;
using ApplicationModels.A04.Interactions;
using Commons.Requests;
using Commons.Responses;
using EntityModels.E03.Photographs;
using EntityModels.E04.Interactions;
using IApplicationServices;
using Microsoft.AspNetCore.Mvc;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WebAPI.Controllers
{
    [Route("[controller]/[action]")]
    public class LikeController:BaseController<int,Like,LikeAM>
    {
        private readonly ILikeService likeService;

        public LikeController(ILikeService apiService) : base(apiService)
        {
            likeService = apiService;
        }
        /// <summary>
        /// 点赞或取消点赞
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageResponse> PhotographLike(PostRequest req)
        {
            var photograph = (await likeService.GetOtherAsync<Guid, Photograph, PhotographAM>(x => x.Id == Guid.Parse(req.Value1))).Data;
            var liked = (await likeService.GetAsync(x => x.PhotographId == Guid.Parse(req.Value1) && x.AppUserId == Guid.Parse(AppUserId))).Data;
            if (liked == null)
            {
               return await likeService.AddAsync(new LikeAM(AppUserId, photograph.Id.ToString(),photograph.AppUserId, photograph.Name));
            }
            else
            {
                return await likeService.DeleteByIdAsync(liked.Id);
            }

        }
        /// <summary>
        /// 获取用户点赞列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public override Task<DatasResponse<LikeAM>> GetBySelectAsync([FromQuery] PagingRequest req)
        {
            Guid userId = Guid.Parse(AppUserId);
            if (!string.IsNullOrWhiteSpace(req.ExtendedValue1))
            {
                Guid.TryParse(req.ExtendedValue1, out userId);
            }
            return ApiService.GetBySelectAsync(req.PageIndex, req.PageSize, x => x.AppUserId == userId);
        }
    }
}
